package algorithm

/**
  * 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。
  * 比如，输入字符串"abcdefg"和数字2，该函数将返回左旋转两位得到的结果"cdefgab"。
  */
object ReverseLeftWords {
  def reverseLeftWords(s: String, n: Int): String = {

    val charArray = s.toCharArray;
    var newCharArray = new Array[Char](charArray.length);
    for(i <- 0 to charArray.length-1){
      newCharArray(i) = charArray((i+n)%(charArray.length));
    }
    newCharArray.mkString;
  }
  def reverseLeftWords2(s: String, n: Int): String = {

    val charArray = s.toCharArray;
    var newString = new StringBuilder(charArray.length);
    for(i <- 0 to charArray.length-1){
      newString.append(charArray((i+n)%(charArray.length)));
    }
    newString.toString()
  }

  def reverseLeftWords3(s: String, n: Int): String = {

    s.substring(n).concat(s.substring(0,n));
  }

  def main(args: Array[String]): Unit = {
//    println(reverseLeftWords("abcdefg",2))
    println(reverseLeftWords2("lrloseumgh",6))
//      println(3%4)
  }
}
